{% extends "base/front_micro.html" %}

{% block container %}
    {% set user = g.api.user_get_one_profile(g.username).data %}
    <div class="row clearfix">
        <div class="col-md-4 column">
            <form role="form" id="UploadCoverId" enctype="multipart/form-data" action="{{ url_for('upload.UploadCoverImage') }}" method="post">
                <div class="form-group">
                    <label for="cover">选择封面图片(仅支持jpg/jpeg/png/gif), 图片尺寸最低1200x600px: </label>
                    <input type="file" name="file" id="cover" />
                </div>
                <input type="button" onclick=SubmitUploadCover() value="修改封面" class="btn btn-success" />
            </form>
        </div>
    </div>
{% endblock %}

{% block script %}
<script type="text/javascript">
function SubmitUploadCover() {
    if(document.getElementById("cover").value=="") {
        layer.alert("请选择封面！", {icon: 2, time: 15*1000});
        return;
    }
    var form = new FormData($("#UploadCoverId")[0]);
    console.log(form);
    $.ajax({
        url: "{{ url_for('upload.UploadCoverImage') }}",
        type: 'POST',
        data: form,
        async: false,  
        cache: false,
        processData: false,  // 告诉jQuery不要去处理发送的数据
        contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
        success: function(res) {
            console.log(res);
            if (res.success==true) {
                layer.msg("修改封面成功", {icon: 1, time: 3*1000});
                parent.location.reload();
            } else {
                layer.alert("修改封面失败，请重试提交或报告错误！\n错误原因是：" + res.msg, {icon: 2, time: 15*1000});
            }
        },
        error:function(e){
            layer.alert("服务器错误，请刷新重试或报告错误！", {icon: 2, time: 15*1000});
        }
    });
}
</script>
{% endblock %}